﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="StockTransfer.aspx.cs"
    Inherits="SalesManagement.Modules.Warehouse.StockTransfer" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Phiếu chuyển kho</title>
    <style type="text/css">
        .x-grid3-cell-inner
        {
            font-family: "segoe ui" ,tahoma, arial, sans-serif;
        }
        .x-grid-group-hd div
        {
            font-family: "segoe ui" ,tahoma, arial, sans-serif;
        }
        .x-grid3-hd-inner
        {
            font-family: "segoe ui" ,tahoma, arial, sans-serif;
            font-size: 12px;
        }
        .x-grid3-body .x-grid3-td-Cost
        {
            background-color: #f1f2f4;
        }
        .x-grid3-summary-row .x-grid3-td-Cost
        {
            background-color: #e1e2e4;
        }
        .total-field
        {
            background-color: #fff;
            font-weight: bold !important;
            color: #000;
            border: solid 1px silver;
            padding: 1px;
            margin-right: 1px;
            text-align: right;
        }
        .search-item
        {
            font: normal 11px tahoma, arial, helvetica, sans-serif;
            padding: 3px 10px 3px 10px;
            border: 1px solid #fff;
            border-bottom: 1px solid #eeeeee;
            white-space: normal;
            color: #555;
        }
        .search-item h3
        {
            display: block;
            font: inherit;
            font-weight: bold;
            color: #222;
        }
        .search-item h3 span
        {
            float: right;
            font-weight: normal;
            margin: 0 0 5px 5px;
            width: 100px;
            display: block;
            clear: none;
        }
    </style>

    <script src="/Javascript/Global.js" type="text/javascript"></script>

    <script language="javascript" type="text/javascript">
        function DocumentReady() {
            var obj = document.getElementById('ResourceManager1');
            MethodNamespace.DocumentReady({
                success: function() {
                    txtVoucherNo.focus();
                    if (hdDocumentID.getValue() == '') {
                        AddTenRecord();
                        btnSave.setDisabled(true);
                        btnDelete.setDisabled(true);
                    }

                }
            });
        }
        function AddTenRecord() {
            for (var i = 0; i < 1; i++) {
                var newIndex = grDetail.addRecord({});
                var record = grDetail.store.getAt(newIndex);
                record.commit();
            }
        }

        function SelectItem(combo, record, index) {

            var Record = grDetail.selModel.selections.items[0];
            if (record == undefined || record == null) {
                Record.set('ProductId', '');
                Record.set('ProductCode', '');
                Record.set('ProductName', '');
                Record.set('UnitId', '');
                Record.set('UnitPrice', '');
                Record.set('Quantity', '');
                Record.set('UnitName', '');
                Record.commit();
            }
            Record.set('ProductId', record.data.ProductId);
            Record.set('ProductCode', record.data.ProductCode);
            Record.set('ProductName', record.data.ProductName);
            Record.set('UnitId', record.data.StockUnit);
            Record.set('UnitPrice', record.data.InputPrice);
            Record.set('UnitName', record.data.UnitName);
            var QuanTity = Record.get('Quantity');
            if (QuanTity <= 0)
                QuanTity = 1;
            Record.set('Quantity', QuanTity);
        }
        function BlurQuantity(el) {
        alert(el.value)
            el.focus();
        }
        function BeforeEdit(cell) {
            SaveVoucher();
            row = cell.row;
            column = cell.column;
            record = cell.record;
            if (column == 0) {
                record.set('ProductId', '-1');
                record.set('ProductCode', '');
                record.set('ProductName', '');
                return true;
            }
            if (record.data.ProductId == '') {
                record.set('ProductId', '');
                record.set('ProductCode', '');
                record.set('ProductName', '');
                record.set('UnitId', '');
                record.set('UnitPrice', '');
                record.set('Quantity', '');
                record.set('UnitName', '');
                StartEdit_Grid(cell.grid, row, 0);
                return false;
            } else {
                var records = grDetail.store.getModifiedRecords();
                var total = 0;
                for (var i = 0; i < records.length; i++) {

                    if (records[i].data.ProductId == record.data.ProductId) {
                        total++;
                    }
                }
                if (total > 1) {
                    record.set('ProductId', '');
                    record.set('ProductCode', '');
                    record.set('ProductName', '');
                    record.set('UnitId', '');
                    record.set('UnitPrice', '');
                    record.set('Quantity', '');
                    record.set('UnitName', '');
                    StartEdit_Grid(cell.grid, row, 0);
                    return false;
                }
            }
        }
        /*
        Thuc hien lưu
        */
        function SaveVoucher() {
            if (!CheckBeforSave())
                return;
            MethodNamespace.SaveVoucher({
                success: function(status) {
                    if (status) {
                        btnAdd.setDisabled(true);
                        btnSave.setDisabled(false);
                        btnDelete.setDisabled(false);

                    } else {
                        vnMessageBox('Thông báo !', 'Không tạo được phiếu chuyển kho. Vui lòng tạo lại', Ext.MessageBox.OK, 'txtVoucherNo.focus(true, 10);', Ext.MessageBox.INFO, 1);
                    }
                }
            });
        }
        function SaveProcess(cell) {
            if (!checkVoucherDetail())
                return;
            var JsonDetail = grDetail.store.getChangedData();
            MethodNamespace.SaveVoucherDetail(JsonDetail, {
                success: function(status) {
                    if (status) {
                        btnAdd.setDisabled(false);
                        vnMessageBox('Thông báo !', 'Lưu thành công', Ext.MessageBox.OK, 'txtVoucherNo.focus(true, 10);', Ext.MessageBox.INFO, 1);
                    }
                }
            });
        }
        function checkVoucherDetail() {
            var strMesseger = '', fn = '';
            var records = grDetail.store.getModifiedRecords();
            if (records.length <= 0 && hdDocumentID.getValue() == '')
                strMesseger += '<br/>nhập chi tiết phiếu trước khi lưu';
            for (var i = 0; i < records.length; i++) {
                for (var j = i + 1; j < records.length; j++) {
                    if (records[i].data.ProductId == records[j].data.ProductId) {
                        strMesseger += "Sản phẩm không được trùng nhau<br />";
                        break;
                    }
                }
            }
            if (strMesseger != '') {
                vnMessageBox('Thông báo !', strMesseger, Ext.MessageBox.OK, fn, Ext.MessageBox.INFO, 1);
                return false;
            }
            return true;
        }

        function CheckBeforSave() {
            var strMesseger = '', fn = '';

            if (txtVoucherNo.getValue() == '') {
                strMesseger += 'Mã phiếu không được trống';
                fn = 'txtVoucherNo.focus(true, 10);';
            }
            if (txtVoucherDate.getValue() == '') {
                strMesseger += '<br/>Ngày nhập không được trống';
                fn = 'txtVoucherDate.focus(true, 10);';
            }
            if (cboFromWarehouse.getValue() == '') {
                strMesseger += '<br/>Vui lòng chọn từ kho';
                fn = 'cboFromWarehouse.focus(true, 10);';
            }
            if (cboToWarehouse.getValue() == '') {
                strMesseger += '<br/>Vui lòng chọn đến kho';
                fn = 'cboToWarehouse.focus(true, 10);';
            }
            if (cboToWarehouse.getValue() == cboFromWarehouse.getValue()) {
                strMesseger += '<br/>Vui lòng chọn từ kho và đến kho không được giống nhau';
                fn = 'cboToWarehouse.focus(true, 10);';
            }

            if (strMesseger != '') {
                vnMessageBox('Thông báo !', strMesseger, Ext.MessageBox.OK, fn, Ext.MessageBox.INFO, 1);
                return false;
            }
            return true;
        }
        function NewMasterProcess() {
            MethodNamespace.NewDataProcess({
                success: function(resul) {
                    grDetail.removeAll();
                    AddTenRecord();
                    btnAdd.setDisabled(true);
                    btnSave.setDisabled(true);
                    btnDelete.setDisabled(true);
                }
            });
        }
        function DeleteProcess() {
            vnMessageBoxConfirm('Thông báo !', 'Bạn có muốn xóa không?', Ext.MessageBox.OKCANCEL, 'DeleteVoucher()', '', Ext.MessageBox.QUESTION, 1);
        }
        function DeleteVoucher() {
            var voucherid = hdDocumentID.Value;
            if (voucherid != null || voucherid != "") {
                MethodNamespace.DeleteProcess({
                    success: function(resul) {
                        if (resul) {
                            vnMessageBox('Thông báo !', 'Xóa thành công', Ext.MessageBox.OK, 'NewMasterProcess();', Ext.MessageBox.INFO, 1);
                        }
                        else {
                            vnMessageBox('Thông báo !', 'Xóa không thành công', Ext.MessageBox.OK, '', Ext.MessageBox.INFO, 1);
                        }
                    }
                });
            }
            else {
                vnMessageBox('Thông báo !', 'Phiếu chuyển kho không tồn tại', Ext.MessageBox.OK, '', Ext.MessageBox.INFO, 1);
            }
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <ext:ResourceManager ID="ResourceManager1" runat="server" DirectMethodNamespace="MethodNamespace">
        <Listeners>
            <DocumentReady Handler="DocumentReady();" Delay="100" />
        </Listeners>
    </ext:ResourceManager>
    <ext:Store ID="storeWarehouse" runat="server">
        <Reader>
            <ext:JsonReader IDProperty="WarehouseId">
                <Fields>
                    <ext:RecordField Name="WarehouseId" />
                    <ext:RecordField Name="WarehouseName" />
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <ext:Store ID="storeEmployee" runat="server">
        <Reader>
            <ext:JsonReader IDProperty="EmployeeId">
                <Fields>
                    <ext:RecordField Name="EmployeeId" />
                    <ext:RecordField Name="EmployeeName" />
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <ext:Store runat="server" ID="stUnit">
        <Reader>
            <ext:JsonReader IDProperty="UnitId">
                <Fields>
                    <ext:RecordField Name="UnitId">
                    </ext:RecordField>
                    <ext:RecordField Name="UnitName">
                    </ext:RecordField>
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <ext:Store ID="stDetail" runat="server">
        <Reader>
            <ext:JsonReader IDProperty="VoucherDetailId">
                <Fields>
                    <ext:RecordField Name="VoucherDetailId" UseNull="true" />
                    <ext:RecordField Name="ProductId" UseNull="true" />
                    <ext:RecordField Name="ProductCode" UseNull="true" />
                    <ext:RecordField Name="ProductName" UseNull="true" />
                    <ext:RecordField Name="Quantity" UseNull="true" />
                    <ext:RecordField Name="UnitId" UseNull="true" />
                    <ext:RecordField Name="UnitName" UseNull="true" />
                    <ext:RecordField Name="UnitPrice" UseNull="true" />
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <ext:Hidden runat="server" ID="hdDocumentID" Text="" />
    <ext:Viewport ID="ViewPort1" runat="server" Layout="RowLayout">
        <Items>
            <ext:Toolbar ID="tbMain" runat="server">
                <Items>
                    <ext:ToolbarSpacer />
                    <ext:Button ID="btnAdd" runat="server" Text="Tạo mới" Icon="Add">
                        <Listeners>
                            <Click Handler="NewMasterProcess()" />
                        </Listeners>
                    </ext:Button>
                    <ext:Button ID="btnSave" runat="server" Icon="Disk" Text="Lưu">
                        <Listeners>
                            <Click Handler="SaveProcess()" />
                        </Listeners>
                    </ext:Button>
                    <ext:Button ID="btnDelete" runat="server" Text="Xóa" Icon="Delete">
                        <Listeners>
                            <Click Handler="DeleteProcess()" />
                        </Listeners>
                    </ext:Button>
                    <ext:ToolbarSpacer />
                    <ext:ToolbarFill />
                    <ext:TextField ID="txtKeyword" runat="server" EmptyText="Tìm kiếm" />
                    <ext:ToolbarSpacer />
                    <ext:Button ID="btnFind" runat="server" Text="Tìm kiếm" Icon="Find" FormBind="true">
                        <DirectEvents>
                            <Click OnEvent="btnFind_Click" />
                        </DirectEvents>
                    </ext:Button>
                </Items>
            </ext:Toolbar>
            <ext:Panel ID="Panel1" runat="server" Frame="true" Height="100" Layout="FormLayout"
                LabelWidth="100">
                <Items>
                    <ext:Panel ID="pnlFlexEven" runat="server" Layout="HBoxLayout" LabelWidth="100" AnchorHorizontal="99%">
                        <Defaults>
                            <ext:Parameter Name="margins" Value="0 5 0 0" Mode="Value" />
                        </Defaults>
                        <LayoutConfig>
                            <ext:HBoxLayoutConfig Padding="2" Align="Middle" />
                        </LayoutConfig>
                        <Items>
                            <ext:TextField ID="txtVoucherNo" runat="server" FieldLabel="Số phiếu <b style='color:red'>*</b>"
                                Flex="1" LabelWidth="100" AllowBlank="false" RegexText="Giá trị không hợp lệ"
                                ReadOnly="true" BlankText="Bắt buộc nhập giá trị" />
                            <ext:DateField ID="txtVoucherDate" runat="server" FieldLabel="Ngày <b style='color:red'>*</b>"
                                Flex="1" Format="dd/MM/yyyy" />
                        </Items>
                    </ext:Panel>
                    <ext:Panel ID="Panel3" runat="server" Layout="HBoxLayout" LabelWidth="100" AnchorHorizontal="99%">
                        <Defaults>
                            <ext:Parameter Name="margins" Value="0 5 0 0" Mode="Value" />
                        </Defaults>
                        <LayoutConfig>
                            <ext:HBoxLayoutConfig Padding="2" Align="Middle" />
                        </LayoutConfig>
                        <Items>
                            <ext:ComboBox ID="cboFromWarehouse" runat="server" FieldLabel="Từ kho" Editable="false"
                                DataIndex="Title" Flex="1" StoreID="storeWarehouse" DisplayField="WarehouseName"
                                ValueField="WarehouseId">
                            </ext:ComboBox>
                            <ext:ComboBox ID="cboToWarehouse" runat="server" FieldLabel="Đến kho" Editable="false"
                                DataIndex="Title" Flex="1" StoreID="storeWarehouse" DisplayField="WarehouseName"
                                ValueField="WarehouseId">
                            </ext:ComboBox>
                        </Items>
                    </ext:Panel>
                    <ext:Panel ID="Panel4" runat="server" Layout="HBoxLayout" LabelWidth="100" AnchorHorizontal="99%">
                        <Defaults>
                            <ext:Parameter Name="margins" Value="0 5 0 0" Mode="Value" />
                        </Defaults>
                        <LayoutConfig>
                            <ext:HBoxLayoutConfig Padding="2" Align="Middle" />
                        </LayoutConfig>
                        <Items>
                            <ext:ComboBox ID="cboEmployeeOut" runat="server" FieldLabel="Nhân viên giao" Editable="false"
                                DataIndex="Title" Flex="1" StoreID="storeEmployee" DisplayField="EmployeeName"
                                ValueField="EmployeeId">
                            </ext:ComboBox>
                            <ext:ComboBox ID="cboEmployeeIn" runat="server" FieldLabel="Nhân viên nhận" Editable="false"
                                DataIndex="Title" Flex="1" StoreID="storeEmployee" DisplayField="EmployeeName"
                                ValueField="EmployeeId">
                            </ext:ComboBox>
                        </Items>
                    </ext:Panel>
                </Items>
            </ext:Panel>
            <ext:GridPanel ID="grDetail" runat="server" AutoExpandColumn="clProductName" Border="false"
                ClicksToEdit="1" RowHeight="1" ColumnLines="true" StoreID="stDetail">
                <KeyMap>
                    <ext:KeyBinding>
                        <Keys>
                            <ext:Key Code="ESC" />
                        </Keys>
                        <Listeners>
                            <Event Handler="KeyPress_ESC(grDetail)" />
                        </Listeners>
                    </ext:KeyBinding>
                </KeyMap>
                <ColumnModel ID="ColumnModel2" runat="server">
                    <Columns>
                        <ext:Column ColumnID="clProductName" DataIndex="ProductName" Header="Mặt hàng">
                            <Editor>
                                <ext:ComboBox ID="ComboBox1" runat="server" DisplayField="ProductName" ValueField="ProductName"
                                    TypeAhead="true" LoadingText="Tìm kiếm..." ItemSelector="div.search-item" TriggerAction="All"
                                    MinChars="1" HideTrigger="true">
                                    <Listeners>
                                        <Select Fn="SelectItem" />
                                    </Listeners>
                                    <Store>
                                        <ext:Store ID="Store1" runat="server" AutoLoad="false">
                                            <Proxy>
                                                <ext:HttpProxy Method="POST" Url="../List/SearProduct.ashx?type=product" DisableCaching="true" />
                                            </Proxy>
                                            <Reader>
                                                <ext:JsonReader Root="SearProduct" TotalProperty="total">
                                                    <Fields>
                                                        <ext:RecordField Name="ProductId" />
                                                        <ext:RecordField Name="ProductCode" />
                                                        <ext:RecordField Name="InputPrice" />
                                                        <ext:RecordField Name="InputUnit" />
                                                        <ext:RecordField Name="StockUnit" />
                                                        <ext:RecordField Name="ProductName" />
                                                        <ext:RecordField Name="WarehouseId" />
                                                    </Fields>
                                                </ext:JsonReader>
                                            </Reader>
                                        </ext:Store>
                                    </Store>
                                    <Template ID="Template1" runat="server">
                                        <Html>
                                        <tpl for=".">                                        
						                  <div class="search-item">
							                 <h3><span>{ProductCode}</span>{ProductName}</h3>
						                  </div>
					                   </tpl>
                                        </Html>
                                    </Template>
                                </ext:ComboBox>
                            </Editor>
                        </ext:Column>
                        <ext:Column DataIndex="Quantity" Header="Số lượng" Align="Right">
                            <Editor>
                                <ext:NumberField ID="txtQuantity" runat="server" MinValue="1" MaxLengthText="18"
                                    SelectOnFocus="true" LabelAlign="Right">
                                    <Listeners>
                                        <Blur Fn="BlurQuantity" />
                                    </Listeners>
                                </ext:NumberField>
                            </Editor>
                        </ext:Column>
                        <ext:Column DataIndex="UnitId" Header="Đơn vị tính">
                            <Renderer Handler="return Render(value,'stUnit','UnitName');" />
                            <Editor>
                                <ext:ComboBox runat="server" ID="cbUnit" StoreID="stUnit" TriggerAction="All" DisplayField="UnitName"
                                    ValueField="UnitId">
                                </ext:ComboBox>
                            </Editor>
                        </ext:Column>
                    </Columns>
                </ColumnModel>
                <SelectionModel>
                    <ext:RowSelectionModel SingleSelect="true" MoveEditorOnEnter="false">
                        <Listeners>
                            <RowSelect Fn="GridRowSelect" Delay="100" />
                        </Listeners>
                    </ext:RowSelectionModel>
                </SelectionModel>
                <Listeners>
                    <BeforeEdit Fn="BeforeEdit" />
                </Listeners>
                <View>
                    <ext:GridView MarkDirty="false" />
                </View>
                <DirectEvents>
                    <AfterEdit OnEvent="grDetail_OnAfterEdit" />
                </DirectEvents>
            </ext:GridPanel>
        </Items>
    </ext:Viewport>
    </form>
</body>
</html>
